package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONArray;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.constant.DeviceFlagEnum;
import com.wtwd.campus.entity.DeviceFlag;
import com.wtwd.campus.entity.Plmn;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.dto.BathLockCardByOrgCodeDto;
import com.wtwd.campus.model.dto.BathLockDeviceDto;
import com.wtwd.campus.model.dto.BathLockDto;
import com.wtwd.campus.model.dto.BathLockNetDto;
import com.wtwd.campus.model.vo.DeviceFlagVo;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.service.DeviceFlagService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mjy
 * @date 2023/01/31
 * @description
 */
@Api(tags = "设备开关模块")
@RestController
@RequestMapping("/device-flag")
public class DeviceFlagController {


    private static final Logger logger = LoggerFactory.getLogger(DeviceFlagController.class);


    @Resource
    private DeviceInfoService deviceInfoService;

    @Resource
    private DeviceFlagService deviceFlagService;

    @Resource
    private SysUserService sysUserService;


    /**
     * 获取设备锁卡情况
     * 条件设备组织
     * 是否锁卡
     *
     * @param orgCode
     * @param flag
     * @return
     */
    @ApiOperation(value = "获取设备锁卡情况", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgCode", value = "组织code", required = true, dataType = "String")
            , @ApiImplicitParam(name = "flag", value = "0关闭1打开", required = true, dataType = "int")
    })
    @GetMapping("/getDeviceLockCardInfoByOrgCode")
    public ResultVO<List<DeviceFlagVo>> getDeviceLockCardInfoByOrgCode(@RequestParam("orgCode") String orgCode,
                                                                       @RequestParam("flag") Integer flag) {
        logger.info("getDeviceLockCardInfoByOrgCode orgCode={},flag={}", orgCode, flag);
        List<DeviceFlagVo> deviceInfos = deviceInfoService.getDeviceLockCardInfoByOrgCode(orgCode, flag);
        return ResultVO.ok(deviceInfos);
    }

    /**
     * 获取设备锁机情况
     *
     * @param orgCode
     * @param flag
     * @return
     */
    @ApiOperation(value = "获取设备锁机情况", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgCode", value = "组织code", required = true, dataType = "String")
            , @ApiImplicitParam(name = "flag", value = "0关闭1打开", required = true, dataType = "int")
    })
    @GetMapping("/getDeviceLockDeviceInfoByOrgCode")
    public ResultVO<List<DeviceFlagVo>> getDeviceLockDeviceInfoByOrgCode(@RequestParam("orgCode") String orgCode,
                                                                         @RequestParam("flag") Integer flag) {
        logger.info("getDeviceLockDeviceInfoByOrgCode orgCode={},flag={}", orgCode, flag);
        List<DeviceFlagVo> deviceInfos = deviceInfoService.getDeviceLockDeviceInfoByOrgCode(orgCode, flag);
        return ResultVO.ok(deviceInfos);
    }


    /**
     * 获取设备锁网情况
     *
     * @param orgCode
     * @param flag
     * @return
     */
    @ApiOperation(value = "获取设备锁网情况", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgCode", value = "组织code", required = true, dataType = "String")
            , @ApiImplicitParam(name = "flag", value = "0关闭1打开", required = true, dataType = "int")
    })
    @GetMapping("/getDeviceLockNetInfoByOrgCode")
    public ResultVO<List<DeviceFlagVo>> getDeviceLockNetInfoByOrgCode(@RequestParam("orgCode") String orgCode,
                                                                      @RequestParam("flag") Integer flag) {
        logger.info("getDeviceLockNetInfoByOrgCode orgCode={},flag={}", orgCode, flag);
        List<DeviceFlagVo> deviceInfos = deviceInfoService.getDeviceLockNetInfoByOrgCode(orgCode, flag);
        return ResultVO.ok(deviceInfos);
    }

    /**
     * 根据设备id查询设备锁开关详情
     *
     * @param deviceId
     * @return
     */
    @ApiOperation(value = "根据设备id查询设备锁开关详情", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "0关闭1打开", required = true, dataType = "int")
    })
    @GetMapping("/getDeviceFlagByDeviceId")
    public ResultVO<DeviceFlag> getDeviceFlagByDeviceId(@RequestParam("deviceId") Integer deviceId) {
        logger.info("getDeviceFlagByDeviceId deviceId={}", deviceId);
        DeviceFlag deviceFlag = deviceFlagService.queryDeviceSwitch(deviceId);
        if (deviceFlag==null){
            deviceFlag = new DeviceFlag();
            deviceFlag.setCardFlag(DeviceFlagEnum.CLOSE.getFlag());
            deviceFlag.setLockNetFlag(DeviceFlagEnum.CLOSE.getFlag());
            deviceFlag.setLockDeviceFlag(DeviceFlagEnum.CLOSE.getFlag());
        }
        return ResultVO.ok(deviceFlag);
    }


    /**
     * 设备批量锁卡
     *
     * @param bathLockDto
     * @return
     */
    @ApiOperation(value = "设备批量锁卡", notes = "")
    @PostMapping("/bathLockCard")
    public ResultVO bathLockCard(@RequestBody BathLockDto bathLockDto) {
        SysUser sysUser = sysUserService.getCurrentLoginUser();
        logger.info("bathLockCard bathLockDto={}", bathLockDto.toString());
        deviceFlagService.bathLockCard(bathLockDto.getLockDtos(), sysUser);
        return ResultVO.ok();
    }

    /**
     * 设备批量锁机
     *
     * @param bathLockDeviceDto
     * @return
     */
    @ApiOperation(value = "设备批量锁机", notes = "")
    @PostMapping("/bathLockDevice")
    public ResultVO bathLockDevice(@RequestBody BathLockDeviceDto bathLockDeviceDto) {
        SysUser sysUser = sysUserService.getCurrentLoginUser();
        logger.info("bathLockDevice bathLockDeviceDto={}", bathLockDeviceDto.toString());
        deviceFlagService.bathLockDevice(bathLockDeviceDto.getLockDtos(), sysUser);
        return ResultVO.ok();
    }

    /**
     * 设备批量锁网
     *
     * @param bathLockNetDto
     * @return
     */
    @ApiOperation(value = "设备批量锁网", notes = "")
    @PostMapping("/bathLockNet")
    public ResultVO bathLockNet(@RequestBody BathLockNetDto bathLockNetDto) {
        SysUser sysUser = sysUserService.getCurrentLoginUser();
        logger.info("bathLockNet bathLockNetDto={}", bathLockNetDto.toString());
        String plmns = bathLockNetDto.getPlmns();
        if (StringUtils.isBlank(plmns)) {
            return ResultVO.error(RespCode.PARAMETERIS_MiSSING);
        }
        List<Plmn> plmnList = deviceFlagService.MccMncByPlmns(plmns, BathLockNetDto.PLMNS_SEPARATE);
        deviceFlagService.bathLockNet(bathLockNetDto.getLockDtos(), plmnList, sysUser);
        return ResultVO.ok();
    }


    /**
     * 批量根据客户code锁卡
     *
     * @param data
     * @return
     */
/*    @ApiOperation(value = "批量根据客户code锁卡", notes = "")
    @PostMapping("/bathLockCardByOrgCode")
    public ResultVO bathLockCardByOrgCode(@RequestBody BathLockCardByOrgCodeDto data) {
        logger.info("bathLockCardByOrgCode data={}", data.toString());
        BathLockDto bathLockDto = deviceFlagService.dealLockCardData(data.getAll(), data.getDeviceIds(), data.getOrgCode());
        return bathLockCard(bathLockDto);
    }*/


}
